package com.tedu.boot.config;





import com.tedu.util.response.AppCode;
import com.tedu.util.response.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @date 2023/4/19
 * @author liuhuan
 */
@RestControllerAdvice(basePackages = {"com.tedu.adapter.controller"})
@Slf4j
public class GlobalExceptionHandler {
    /*
    绑定异常，错误码：1002
     */
    @ExceptionHandler({BindException.class})
    public ResultVo bindException(BindException e){
        ObjectError objectError=e.getBindingResult().getAllErrors().get(0);
        return new ResultVo(AppCode.VALIDATE_ERROR,objectError.getDefaultMessage());
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResultVo methodArgumentNotValidException(MethodArgumentNotValidException e){
        ObjectError objectError=e.getBindingResult().getAllErrors().get(0);
        return new ResultVo(AppCode.VALIDATE_ERROR,objectError.getDefaultMessage());
    }

}
